package com.yj.change;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.yj.change.adapter.AppListAdapter;
import com.yj.change.data.MyAppInfo;
import com.yj.change.data.Utils;
import com.yj.change.db.HookDB;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private AppListAdapter appListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.listView = findViewById(R.id.listView);
        this.appListAdapter = new AppListAdapter();
        this.listView.setAdapter(appListAdapter);
        this.initListener();
        this.initData();
    }

    private void initListener() {
        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                String packageName = appListAdapter.getItem(i).getPackageName();

                if (HookDB.getIntance().readHookApps().contains(packageName)) {
                    HookDB.getIntance().readHookApps().remove(packageName);
                } else {
                    HookDB.getIntance().readHookApps().add(packageName);
                }
                HookDB.getIntance().saveHook();
                appListAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "添加HOOK成功", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initData() {
        Utils.initAppList(this, new Utils.AppCallBack() {
            @Override
            public void onSuccess(List<MyAppInfo> infos) {
                appListAdapter.addInfos(infos);
                appListAdapter.notifyDataSetChanged();
            }
        });

    }
}
